package com.cuz.daileetcode;

import javafx.util.Pair;

import java.util.*;
import java.util.stream.Collectors;

public class Day50 {

    static class ZigzagPrintMatrix {

        public static void main(String[] args) {
            int[][] arr = {
                    new int[]{0, 1, 2, 3},
                    new int[]{4, 5, 6, 7},
                    new int[]{8, 9, 10, 11}
            };
//
//            printEdge(arr, 1, 0, 0, 1, false);
//            printEdge(arr, 1, 0, 0, 1, true);
            solution1(arr);
        }

        public static void solution1(int[][] matrix) {
            if (matrix == null) {
                return;
            }
            int startX = 0;
            int startY = 0;
            int endX = 0;
            int endY = 0;
            boolean flag = false;
            while (endX <= startX) {
                printEdge(matrix, startX, startY, endX, endY, flag);
                flag = !flag;
                //可以向下就一直下
                if (startX < matrix.length - 1) {
                    startX++;
                } else {
                    //反之向右
                    startY++;
                }
                //可以向右就一直右
                if (endY < matrix[0].length - 1) {
                    endY++;
                } else {
                    //反之向下
                    endX++;
                }
            }
        }

        private static void printEdge(int[][] matrix, int startX, int startY, int endX, int endY, boolean flag) {
            //为true 从 右上打印到左下
            if (flag) {
                while (endX <= startX) {
                    System.out.print(matrix[endX][endY] + " ");
                    endX++;
                    endY--;
                }
            } else {
                while (startX >= endX) {
                    System.out.print(matrix[startX][startY] + " ");
                    startX--;
                    startY++;
                }
            }
        }
    }

    static class TopkMostTimes {

        public static void main(String[] args) {
            List<Character> abamcb = solution1("abamcbbmmm", 2);
            System.out.println(abamcb);
        }
        static List<Character> solution1(String str, int k) {
            if (str == null || "".equals(str)) {
                return Collections.emptyList();
            }
            char[] charArray = str.toCharArray();
            HashMap<Character, Integer> memory = new HashMap<>();
            for (char temp : charArray) {
                memory.put(temp, memory.getOrDefault(temp, 0)+1);
            }
            PriorityQueue<Pair<Character, Integer>> heap = new PriorityQueue<>(Comparator.comparing(Pair::getValue));
            for (char temp : charArray) {
                if (heap.size() <= k-1) {
                    heap.add(new Pair<>(temp, memory.get(temp)));
                } else {
                    Integer countHeapTop = heap.peek().getValue();
                    Integer curCharCount = memory.get(temp);
                    if (countHeapTop < curCharCount) {
                        heap.poll();
                        heap.add(new Pair<>(temp, curCharCount));
                    }
                }
            }
            return heap.stream().map(Pair::getKey).collect(Collectors.toList());
        }
    }
}
